"""Update entity for VeSync.."""

from pyvesync.base_devices.vesyncbasedevice import VeSyncBaseDevice

from homeassistant.components.update import UpdateDeviceClass, UpdateEntity
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant, callback
from homeassistant.helpers.dispatcher import async_dispatcher_connect
from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback

from .const import DOMAIN, VS_COORDINATOR, VS_DEVICES, VS_DISCOVERY, VS_MANAGER
from .coordinator import VeSyncDataCoordinator
from .entity import VeSyncBaseEntity


async def async_setup_entry(
    hass: HomeAssistant,
    config_entry: ConfigEntry,
    async_add_entities: AddConfigEntryEntitiesCallback,
) -> None:
    """Set up update entity."""
    coordinator = hass.data[DOMAIN][VS_COORDINATOR]

    @callback
    def discover(devices: list[VeSyncBaseDevice]) -> None:
        """Add new devices to platform."""
        _setup_entities(devices, async_add_entities, coordinator)

    config_entry.async_on_unload(
        async_dispatcher_connect(hass, VS_DISCOVERY.format(VS_DEVICES), discover)
    )

    _setup_entities(
        hass.data[DOMAIN][VS_MANAGER].devices, async_add_entities, coordinator
    )


@callback
def _setup_entities(
    devices: list[VeSyncBaseDevice],
    async_add_entities: AddConfigEntryEntitiesCallback,
    coordinator: VeSyncDataCoordinator,
) -> None:
    """Check if device is a light and add entity."""

    async_add_entities(
        VeSyncDeviceUpdate(
            device=device,
            coordinator=coordinator,
        )
        for device in devices
    )


class VeSyncDeviceUpdate(VeSyncBaseEntity, UpdateEntity):
    """Representation of a VeSync device update entity."""

    _attr_device_class = UpdateDeviceClass.FIRMWARE

    @property
    def installed_version(self) -> str | None:
        """Return installed_version."""
        return self.device.current_firm_version

    @property
    def latest_version(self) -> str | None:
        """Return latest_version."""
        return self.device.latest_firm_version
